//
//  ByCoreViewShare.m
//  beibei_ios
//
//  Created by mc on 16/5/5.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByCoreViewShare.h"


#define CELL @"cell"
#define HEADER @"header"
#define FOOTER @"footer"




@implementation CustomCell

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        CGFloat height = SCREEN_WIDTH / 4.0;
        
        self.aImageView = [[UIImageView alloc] init];
        [self addSubview:_aImageView];
        [_aImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(10);
            make.centerX.equalTo(self.mas_centerX).offset(0);
            make.size.mas_equalTo(CGSizeMake(height/ 2, height/2));
        }];
        _aImageView.userInteractionEnabled = YES;
        
        self.aLable = [[UILabel alloc] init];
        _aLable.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_aLable];
        [_aLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-10);
            make.centerX.equalTo(self.mas_centerX).offset(0);
            make.size.mas_equalTo(CGSizeMake(height, 25));
        }];
        _aLable.font = FONT(13);
        _aLable.userInteractionEnabled = YES;
        _aLable.textColor = [UIColor grayColor];

      }
    return self;
}
@end


@implementation HeaderView

- (id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.headerLable = [[UILabel alloc] init];
        [self addSubview:_headerLable];
        _headerLable.textAlignment = NSTextAlignmentLeft;
        _headerLable.font = FONT(13);
        [_headerLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(20);
            make.centerY.equalTo(self.mas_centerY).offset(0);
            make.size.mas_equalTo(CGSizeMake(200, 25));
        }];
        self.headerLable.textColor = [UIColor grayColor];
        
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 29, SCREEN_WIDTH, 0.5)];
//        line.backgroundColor = LINE_CELL_COLOR;
        [self addSubview:line];
        
    }
    return self;
}

@end



@implementation FooterView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.footerLable = [[UILabel alloc] init];
        [self addSubview:_footerLable];
        _footerLable.textAlignment = NSTextAlignmentCenter;
        [_footerLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.mas_centerX).offset(0);
            make.centerY.equalTo(self.mas_centerY).offset(0);
            make.size.mas_equalTo(CGSizeMake(200, 25));
        }];
        self.footerLable.textColor = [UIColor grayColor];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
//        line.backgroundColor = LINE_CELL_COLOR;
        [self addSubview:line];
    
    }
    
    return self;
}

@end


@interface ByCoreViewShare ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout/*,UMSocialUIDelegate*/>{
    NSArray *_itemsAry;
    NSArray *_imagesAry;
}


@property (nonatomic, strong) UIView *shareView; //整体视图
@property (nonatomic, strong) UICollectionView *collectionView; //分享试图

@end

@implementation ByCoreViewShare

//直接进行分享
- (void)share:(UIView *)sender{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.shareView];
    [window addSubview:self.collectionView];
//    [UIView animateWithDuration:0.3f animations:^{
//        _shareView.alpha = 1;
//    }];
    _shareView.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        _shareView.alpha = 0.5;
        self.collectionView.bottom = SCREEN_HEIGHT;
    }];


}

- (instancetype)initWithView:(BOOL)isShow{
    self = [super init];
    if (self && isShow) {
        _shareView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _shareView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
        _shareView.alpha = 0;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [_shareView addGestureRecognizer:tap];
        
        [self _initData];
        [self _createCollectionView];
    }
    return self;
}


//加载数据用于确定分享类型
- (void)_initData{
    _itemsAry = @[@"QQ",@"微信",@"朋友圈"];
    _imagesAry = @[@"QQ",@"微信",@"朋友圈"];
    
}


- (void)_createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    CGFloat width = SCREEN_WIDTH / _itemsAry.count;
    CGFloat height = SCREEN_WIDTH / 4.0;
    // 设置单元格尺寸
    layout.itemSize = CGSizeMake(width, height);
    
    
    //设置区头大小
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 30);
    layout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 40);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, _shareView.bottom, SCREEN_WIDTH, height + 70) collectionViewLayout:layout];
    self.collectionView.top = SCREEN_HEIGHT;
    [self addSubview:_collectionView];
    // 设置代理和数据源方法
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.scrollEnabled = NO;
    _collectionView.userInteractionEnabled = YES;
    //给集合视图注册一个类型的cell
    [_collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:CELL];
    //注册区头视图
    [_collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HEADER];
    
    [_collectionView registerClass:[FooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:FOOTER];
    
    
    
}

    
- (void)tapAction{
        [UIView animateWithDuration:0.3 animations:^{
            self.shareView.alpha = 0;
            self.collectionView.alpha = 0;
        } completion:^(BOOL finished) {
            self.shareView.hidden = YES;
            [self.shareView removeFromSuperview];
            self.collectionView.hidden = YES;
            [self.collectionView removeFromSuperview];

        }];
}



#pragma mark - collectionView 代理
// 定义展示的UICollectionViewCell的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
// 定义展示的Section的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}
// 每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = [_itemsAry objectAtIndex:indexPath.row];
    CustomCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL forIndexPath:indexPath];
    cell.aImageView.image = [UIImage imageNamed:string];
    cell.aLable.text = string;
    return cell;
}

// 单元格选中事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
//    NSString *shareText = @"萌屋分享";
//    NSString *url = @"http://sunsun.itboye.com/index.php/Api/Picture/index?id=1666";
//    
//    // 微信处理
//    [UMSocialWechatHandler setWXAppId:WEIXIN_APPID appSecret:WEIXIN_APPSECRET url:url];
//    [UMSocialQQHandler setQQWithAppId:QQ_APPID appKey:QQ_APPKEY url:url];
//    // TODO: 分享链接的问题®
//    // 微博分享的处理
//    [UMSocialData defaultData].extConfig.sinaData.shareText = shareText;
//    [UMSocialData defaultData].extConfig.sinaData.urlResource.url = url;
//    [UMSocialData defaultData].extConfig.sinaData.urlResource.resourceType = UMSocialUrlResourceTypeImage;
//    
//    // 微信消息类型
//    [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
//    
//    // 微信朋友圈分享
//    [UMSocialData defaultData].extConfig.wechatTimelineData.title = shareText;
//    [UMSocialData defaultData].extConfig.wechatTimelineData.url = url;
//    
//    // 微信好友分享
//    [UMSocialData defaultData].extConfig.wechatSessionData.title = shareText;
//    [UMSocialData defaultData].extConfig.wechatSessionData.url = url;
//    
//    // qq分享
//    [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
//    [UMSocialData defaultData].extConfig.qqData.title = shareText;
//    [UMSocialData defaultData].extConfig.qzoneData.title = shareText;
//    [UMSocialData defaultData].extConfig.qqData.url = url;
//    [UMSocialData defaultData].extConfig.qzoneData.url = url;
//    UIImageView * imageView  = [[UIImageView alloc] init];
//    imageView.image = [UIImage imageNamed:@"logo"];
//
//    NSString * string = [_itemsAry objectAtIndex:indexPath.row];
//    
//    //qq分享
//    if (indexPath.row == 0) {
//        NSLog(@"%@分享", string);
//        
//        //[ByCoreToolAlert alertError:@"正在申请中"];
//        //return;
//        [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ]
//                                                           content:@"萌屋分享"
//                                                             image:imageView.image//[UIImage getImageFromURL:self.model.main_img]
//                                                          location:nil
//                                                       urlResource:nil
//                                               presentedController:self.viewController
//                                                        completion:^(UMSocialResponseEntity *response) {
//                                                            if (response.responseCode == UMSResponseCodeSuccess) {
//                                                                NSLog(@"分享成功！");
//                                                            }
//                                                        }];
//
//    //微信分享
//    }else if (indexPath.row == 1){
//        NSLog(@"%@分享", string);
//        [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession]
//                                                           content:@"萌屋分享"
//                                                             image:imageView.image
//                                                          location:nil
//                                                       urlResource:nil
//                                               presentedController:self.viewController
//                                                        completion:^(UMSocialResponseEntity *response) {
//                                                            if (response.responseCode == UMSResponseCodeSuccess) {
//                                                                NSLog(@"分享成功！");
//                                                            }
//                                                        }];
// 
//    //朋友圈
//    }else if (indexPath.row == 2){
//        NSLog(@"%@分享", string);
//        [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatTimeline]
//                                                           content:@"萌屋分享"
//                                                             image:imageView.image
//                                                          location:nil
//                                                       urlResource:nil
//                                               presentedController:self.viewController
//                                                        completion:^(UMSocialResponseEntity *response) {
//                                                            if (response.responseCode == UMSResponseCodeSuccess) {
//                                                                NSLog(@"分享成功！");
//                                                            }
//                                                        }];
//
//    }
//    
//    [self tapAction];
}


//设置区头区尾视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    //如果需要展示的区头视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        //从辅助视图的重用队列取出一个可重用的区头视图
        HeaderView * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:HEADER forIndexPath:indexPath];
        headerView.headerLable.text = @"分享到";
        
        return headerView;
    }else{
        FooterView * footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:FOOTER forIndexPath:indexPath];
        footerView.footerLable.text = @"取消";
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [footerView addGestureRecognizer:tap];

        return footerView;
    }
}

    
@end
